home *** CD-ROM | disk | FTP | other *** search
- /* AuditDocument.c */
- /*
- * DisplayAudit.c
- * Copyright © 1992-93, Apple Computer Inc. All Rights Reserved.
- * Programmed by Martin Minow,
- * Internet: minow@apple.com
- * AppleLink: MINOW
- */
- #include "DisplayAudit.h"
-
- #ifndef TESTING
- #define TESTING 0
- #endif
- #if TESTING
- void TestAuditStatusLocationUsingAnExtremelyLongFunctionName(void);
- pascal void TestPascalVoidFunction(void);
- pascal short TestPascalShortFunction(void);
- pascal OSErr TestPascalFunctionWithParameters(Ptr, Ptr);
- #endif
-
- static void
- GetTagViewRect(
- register DocumentPtr documentPtr,
- Rect *viewRect
- )
- {
- *viewRect = (**DOC.tagButton).contrlRect;
- viewRect->left = viewRect->right;
- viewRect->right = WINDOW.portRect.right - kScrollBarWidth;
- InsetRect(viewRect, 4, 0);
- }
-
- /*
- * MakeDocumentWindow creates a window that will hold the log information.
- * It then creates/initializes the Audit Record for that window. The strings
- * should be fetched from the resource fork.
- */
- void
- MakeDocumentWindow(void)
- {
- OSErr status;
- register DocumentPtr documentPtr;
- WindowPtr theWindow;
- short windowWidth;
- short windowHeight;
- Point windowTopLeft;
- Rect viewRect;
- SFReply sfReply;
- Str255 windowTitle;
- short listFontNumber;
- StringPtr message;
- Boolean saveAuditEnable;
- FontInfo info;
- short lineHeight;
-
- documentPtr = (DocumentPtr) NewPtrClear(sizeof (DocumentRecord));
- FailNIL(documentPtr, kErrNoMemory);
- TRY {
- AuditFileDialog(
- DLOG_SFPutFile,
- "\pAudit Log File",
- "\pAudit Log",
- &sfReply
- );
- gParameterUpdateNeeded = TRUE;
- DOC.auditIdent = PARM.auditIdent;
- DOC.enableAudit = PARM.enableAudit;
- DOC.scanCount = PARM.auditRecords;
- if (gHasColorQuickDraw) {
- theWindow = GetNewCWindow(
- WIND_Document,
- &DOC.windowRecord,
- (WindowPtr) -1
- );
- }
- else {
- theWindow = GetNewWindow(
- WIND_Document,
- &DOC.windowRecord,
- (WindowPtr) -1
- );
- }
- FailNIL(theWindow, kErrCreateTextWindow);
- if ((long) theWindow != (long) documentPtr) /* Can't happen */
- FailNIL(NULL, kErrCreateTextWindow); /* It did! */
- SetPort(WINDOW_PTR); /* Do this first */
- GetWTitle(WINDOW_PTR, windowTitle);
- BlockMove(
- &DOC.auditIdent,
- &windowTitle[windowTitle[0] + 1 - sizeof (OSType)],
- sizeof (OSType)
- );
- SetWTitle(WINDOW_PTR, windowTitle);
- windowWidth = width(qd.screenBits.bounds) / 2;
- if (windowWidth < kMinWindowWidth)
- windowWidth = kMinWindowWidth;
- windowHeight = height(qd.screenBits.bounds) - (GetMBarHeight() * 2) - 4;
- if (windowHeight < kMinWindowHeight)
- windowHeight = kMinWindowHeight;
- windowTopLeft.h = qd.screenBits.bounds.left + 2;
- windowTopLeft.v = qd.screenBits.bounds.top + GetMBarHeight() * 2 + 2;
- TextFont(applFont);
- TextSize(kApplFontSize);
- TextFace(normal);
- DOC.tagButton = GetNewControl(CNTL_TagButton, WINDOW_PTR);
- FailNIL(DOC.tagButton, kErrCreateTextWindow);
- DOC.tagLine = (**DOC.tagButton).contrlRect.bottom + 6;
- GetTagViewRect(documentPtr, &viewRect);
- GetFNum(PARM.fontName, &listFontNumber);
- TextFont(listFontNumber);
- TextSize(PARM.fontSize);
- GetFontInfo(&info);
- /*
- * Shrink the window height so an integral number of lines are shown.
- */
- lineHeight = info.ascent + info.descent + info.leading;
- windowHeight -= (DOC.tagLine + kScrollBarOffset);
- windowHeight -= (windowHeight % lineHeight);
- windowHeight += (DOC.tagLine + kScrollBarOffset);
- MoveWindow(WINDOW_PTR, windowTopLeft.h, windowTopLeft.v, FALSE);
- SizeWindow(WINDOW_PTR, windowWidth, windowHeight, FALSE);
- BringToFront(WINDOW_PTR);
- ShowWindow(WINDOW_PTR);
- SelectWindow(WINDOW_PTR);
- /*
- * Add the tag button and tag text handle for time-stamping.
- */
- DOC.tagEditHandle = CreateEditHandle(
- &viewRect,
- applFont,
- kApplFontSize,
- MENU_Edit,
- STRN_EditMenu,
- 0 /* No RefCon */
- );
- FailNIL(DOC.tagEditHandle, kErrCreateTextWindow);
- TESetText((Ptr) "•", 1, GetTEHandle(DOC.tagEditHandle));
- /*
- * Build a display log in the window.
- * To do: let the user choose the font and size.
- */
- viewRect = WINDOW.portRect;
- viewRect.top = DOC.tagLine;
- viewRect.right -= kScrollBarOffset;
- viewRect.bottom -= kScrollBarOffset;
- DOC.logListHandle = CreateLog(
- &viewRect,
- listFontNumber,
- PARM.fontSize,
- PARM.logDisplayLines,
- TRUE
- );
- FailNIL(DOC.logListHandle, kErrCreateTextWindow);
- /*
- * Force an update event so the window looks good when it starts.
- * Otherwise, there's a long pause if the Audit record is full.
- */
- BeginUpdate(WINDOW_PTR);
- TextFont(applFont);
- TextSize(kApplFontSize);
- TextFace(normal);
- PenNormal();
- UpdateControls(WINDOW_PTR, WINDOW_PTR->visRgn);
- UpdateDocumentWindow(documentPtr);
- DrawGrowIcon(WINDOW_PTR);
- EndUpdate(theWindow);
- /*
- * Create a log file if requested. (Error is non-fatal).
- */
- if (sfReply.good == FALSE)
- DOC.logFileRefNum = 0;
- else {
- status = CreateOutputFile(
- 'ttxt',
- sfReply.fName,
- sfReply.vRefNum,
- &DOC.logFileRefNum
- );
- if (status != noErr) {
- DOC.logFileRefNum = 0;
- ErrorAlert(status, kErrCreateOutputFile, FALSE);
- }
- else {
- DOC.logFileVRefNum = sfReply.vRefNum;
- pstrcpy(DOC.logFileName, sfReply.fName);
- }
- }
- DOC.auditPtr = GetAuditPtr(DOC.auditIdent);
- if (DOC.auditPtr != NULL)
- message = "\pAudit Record was previously created";
- else {
- DOC.auditPtr = InitAudit(
- DOC.auditIdent,
- PARM.auditRecords,
- DOC.enableAudit,
- FALSE
- );
- message = "\pAudit Record created";
- }
- GetCurrentProcess(&DOC.oldPSN);
- WakeUpAudit(DOC.auditPtr, &DOC.oldPSN);
- saveAuditEnable = EnableAudit(DOC.auditPtr, TRUE);
- Audit(
- DOC.auditPtr,
- 'APPL',
- AuditFormat3(
- kAuditFormatHex,
- kAuditFormatAddress,
- kAuditFormatString
- ),
- DOC.auditIdent,
- DOC.auditPtr,
- message
- );
- EnableAudit(DOC.auditPtr, saveAuditEnable);
- #if TESTING
- AuditString(
- DOC.auditPtr,
- 'Moof',
- "\p1234567890123456789012345678901234567890"
- );
- AuditStatusString(DOC.auditPtr, 'Moof', 1, "\pHello world");
- AuditStatusLocation(DOC.auditPtr, 'Moof', 2);
- TestAuditStatusLocationUsingAnExtremelyLongFunctionName();
- TestPascalVoidFunction();
- TestPascalShortFunction();
- TestPascalFunctionWithParameters(NULL, NULL);
- {
- long i;
- unsigned long foo;
-
- foo = 0x00010203;
- for (i = 0; i < (256 / 4); i++) {
- Audit(
- DOC.auditPtr,
- foo,
- AuditFormat2(kAuditFormatSigned, kAuditFormatHex),
- i,
- foo
- );
- foo += 0x04040404;
- }
- }
- #endif
- }
- CATCH {
- if (DOC.logListHandle != NULL)
- DisposeLog(DOC.logListHandle);
- if (DOC.logFileRefNum != 0) {
- (void) FSClose(DOC.logFileRefNum);
- (void) FSDelete(DOC.logFileName, DOC.logFileVRefNum);
- }
- DisposePtr((Ptr) documentPtr);
- }
- ENDTRY;
- }
-
- void
- TimeStamp(
- register DocumentPtr documentPtr
- )
- {
- Str255 work;
- TEHandle teHandle;
-
- teHandle = GetTEHandle(DOC.tagEditHandle);
- GetIText((**teHandle).hText, work);
- AuditString(DOC.auditPtr, '••••', work);
- }
-
- void
- ProcessAuditDocument(
- register DocumentPtr documentPtr
- )
- {
- register short i;
-
- for (i = 0; i < DOC.scanCount && ReadAudit(DOC.auditPtr, &ENTRY); i++)
- ProcessAuditEntry(documentPtr);
- }
-
- void
- ProcessAuditEntry(
- register DocumentPtr documentPtr
- )
- {
- Str255 message;
- Str255 work;
- short i;
-
- if (ENTRY.lostData > 0) {
- DOC.totalMissedCount += ENTRY.lostData;
- NumToString(ENTRY.lostData, message);
- pstrcat(message, "\p log entries(");
- NumToString(DOC.totalMissedCount, work);
- pstrcat(message, work);
- pstrcat(message, "\p total) missed...");
- DisplayLogString(DOC.logListHandle, message);
- DOC.logIndex += ENTRY.lostData;
- }
- DOC.logIndex += 1;
- message[0] = 0;
- NumToString(DOC.logIndex, work);
- for (i = work[0]; i < 3; i++)
- pstrcat(message, "\p ");
- pstrcat(message, work);
- pstrcat(message, "\p ");
- FormatAuditEntryTimestamp(DOC.auditPtr, &ENTRY, work);
- /*
- * Since we know that the timestamp has a fixed-length format, we can chop
- * out the date to gain some space in the text window. Note the format:
- * 0 1 2
- * 12345678901234567890123
- * 1993.01.14 22:36:57.123
- */
- work[11] = work[0] - 11;
- pstrcat(message, &work[11]);
- pstrcat(message, "\p ");
- FormatAuditEntryData(&ENTRY, work);
- pstrcat(message, work);
- DisplayLogString(DOC.logListHandle, message);
- if (DOC.logFileRefNum != 0) {
- TRY {
- WriteAuditOutputLine(message, DOC.logFileRefNum);
- }
- CATCH {
- ErrorAlert(STATUS, MESSAGE, FALSE);
- GetIndString(work, STRN_Messages, kErrWriteOutputFile);
- AuditStatusString(DOC.auditPtr, 'APPL', STATUS, work);
- CloseAuditOutputFile(
- STATUS,
- DOC.logFileRefNum,
- DOC.logFileVRefNum,
- DOC.logFileName
- );
- DOC.logFileRefNum = 0;
- NO_PROPAGATE;
- gUpdateMenusNeeded = TRUE;
- }
- ENDTRY;
- }
- }
-
- /*
- * These are passed more-or-less intact to the log manager.
- * Changing the EditMenu is not good programming practice.
- */
- void
- DoWindowKeyDown(
- register DocumentPtr documentPtr
- )
- {
- if (DoTextEditEvent(DOC.tagEditHandle, &EVENT))
- AdjustEditMenu(DOC.tagEditHandle);
- }
-
- void
- DoWindowNullEvent(
- register DocumentPtr documentPtr
- )
- {
- DoTextEditEvent(DOC.tagEditHandle, &EVENT);
- }
-
- void
- AdjustDocumentEditMenu(
- register DocumentPtr documentPtr
- )
- {
- AdjustEditMenu(DOC.tagEditHandle);
- }
- void
- DoDocumentEditMenu(
- register DocumentPtr documentPtr,
- short menuItem
- )
- {
- ManageEditMenu(DOC.tagEditHandle, menuItem);
- }
-
- void
- DoContentClick(
- register DocumentPtr documentPtr
- )
- {
- ControlHandle theControl;
- short partCode;
- Point mousePt;
-
- if (DoTextEditEvent(DOC.tagEditHandle, &EVENT))
- AdjustEditMenu(DOC.tagEditHandle);
- else if (DoClickInLog(DOC.logListHandle, &EVENT) == FALSE) {
- mousePt = EVENT.where;
- GlobalToLocal(&mousePt);
- partCode = FindControl(mousePt, WINDOW_PTR, &theControl);
- if (partCode != 0 && theControl == DOC.tagButton) {
- TextFont(applFont);
- TextSize(kApplFontSize);
- TextFace(normal);
- if (TrackControl(theControl, mousePt, NULL) == inButton)
- TimeStamp(documentPtr);
- }
- }
- }
-
- void
- UpdateDocumentWindow(
- register DocumentPtr documentPtr
- )
- {
- Rect viewRect;
- TEHandle teHandle;
-
- DrawGrowIcon(WINDOW_PTR);
- UpdateLog(DOC.logListHandle);
- GetTagViewRect(documentPtr, &viewRect);
- FrameRect(&viewRect);
- teHandle = GetTEHandle(DOC.tagEditHandle);
- TEUpdate(&WINDOW_PTR->portRect, teHandle);
- }
-
- void
- ActivateDocumentWindow(
- register DocumentPtr documentPtr,
- Boolean isActivating
- )
- {
- TEHandle teHandle;
-
- ActivateLog(DOC.logListHandle, isActivating);
- teHandle = GetTEHandle(DOC.tagEditHandle);
- if (isActivating)
- TEActivate(teHandle);
- else {
- TEDeactivate(teHandle);
- }
- }
-
- void
- DisposeDocumentWindow(
- register DocumentPtr documentPtr
- )
- {
- OSErr status;
- short message;
-
- status = noErr;
- if (DOC.tagEditHandle != NULL)
- DisposeEditHandle(DOC.tagEditHandle);
- if (DOC.auditPtr != NULL)
- WakeUpAudit(DOC.auditPtr, &DOC.oldPSN);
- if (DOC.logFileRefNum != 0) {
- TRY {
- CloseAuditOutputFile(
- noErr,
- DOC.logFileRefNum,
- DOC.logFileVRefNum,
- DOC.logFileName
- );
- }
- CATCH {
- status = STATUS;
- message = MESSAGE;
- NO_PROPAGATE;
- }
- ENDTRY;
- }
- CloseWindow(WINDOW_PTR);
- DisposePtr((Ptr) documentPtr);
- if (--gOpenWindowCount <= 0)
- gQuitNow = TRUE;
- FailOSErr(status, message);
- }
-
- void
- DecorateWindow(
- register DocumentPtr documentPtr
- )
- {
- Rect viewRect;
-
- SizeLog(
- DOC.logListHandle,
- width(WINDOW.portRect) - kScrollBarOffset,
- height(WINDOW.portRect) - kScrollBarOffset - DOC.tagLine
- );
- GetTagViewRect(documentPtr, &viewRect);
- RepositionEditItem(DOC.tagEditHandle, &viewRect);
- }
-
- void
- DoDocumentSaveAs(
- register DocumentPtr documentPtr
- )
- {
- if (DOC.logFileRefNum != 0)
- DoDocumentCloseFile(documentPtr);
- TRY {
- PromptAndCreateAuditOutputFile(
- "\pAudit Output File",
- "\pAudit Log",
- 'ttxt',
- &DOC.logFileRefNum,
- &DOC.logFileVRefNum,
- DOC.logFileName
- );
- }
- CATCH {
- ErrorAlert(STATUS, MESSAGE, FALSE);
- NO_PROPAGATE;
- }
- ENDTRY;
- }
-
- void
- DoDocumentCloseFile(
- register DocumentPtr documentPtr
- )
- {
- TRY {
- CloseAuditOutputFile(
- noErr,
- DOC.logFileRefNum,
- DOC.logFileVRefNum,
- DOC.logFileName
- );
- }
- CATCH {
- ErrorAlert(STATUS, MESSAGE, FALSE);
- NO_PROPAGATE;
- }
- ENDTRY;
- DOC.logFileRefNum = 0;
- gUpdateMenusNeeded = TRUE;
- }
-
- #if TESTING
- /*
- * These were used to test the Audit library and are retained because I'm a kind
- * of sentimental guy.
- */
- #define AuditLocation(ident) ( \
- Audit( \
- ((DocumentPtr) FrontWindow())->auditPtr, \
- (ident), \
- AuditFormat1(kAuditFormatLocation) \
- ) \
- )
-
- void
- TestAuditStatusLocationUsingAnExtremelyLongFunctionName(void) {
- AuditLocation('Moof');
- }
-
- pascal void
- TestPascalVoidFunction(void)
- {
- AuditLocation('Moof');
- }
-
- pascal short
- TestPascalShortFunction(void)
- {
- AuditLocation('Moof');
- return (123);
- }
- pascal OSErr
- TestPascalFunctionWithParameters(
- Ptr fooPtr,
- Ptr barPtr
- )
- {
- AuditLocation('Moof');
- return ((fooPtr == barPtr) ? noErr : paramErr);
- }
- #endif
-
-